package com.xiaoxi.exam.bean.response;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xiaoxi.exam.bean.enums.WxPayErrorCode;
import com.xiaoxi.exam.bean.enums.WxPayRequestStatus;
import java.nio.charset.StandardCharsets;
import lombok.Data;

@Data
public class WxBaseResponse {

  //返回状态码
  private String return_code;
  //返回信息
  private String return_msg;
  //业务结果
  private String result_code;
  //错误代码
  private String err_code;
  //错误代码描述
  private String err_code_des;

  /**
   * 连接是否成功
   */
  @JsonIgnore
  public boolean isContact() {
    return WxPayRequestStatus.SUCCESS.name().equals(this.return_code);
  }

  public String invalidContactMsg() {
    if (!isContact()) {
      String msg = this.return_code + ":" + this.return_msg;
      return new String(msg.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
    }
    return "";
  }

  /**
   * 业务是否成功
   */
  @JsonIgnore
  public boolean isSuccess() {
    return isContact() && WxPayRequestStatus.SUCCESS.name().equals(this.result_code);
  }

  public String failMsg() {
    return this.result_code + ":" + WxPayErrorCode.getErrorMsg(this.err_code);
  }
}
